package com.ayit.car.controller.car;

import com.ayit.car.common.result.PageResult;
import com.ayit.car.common.result.Result;
import com.ayit.car.pojo.Vehicles;
import com.ayit.car.pojo.dto.VehiclesDTO;
import com.ayit.car.pojo.dto.VehiclesPageQueryDTO;
import com.ayit.car.pojo.vo.VehiclesVO;
import com.ayit.car.service.VehiclesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car/vehicles")
@Api(tags = "车辆信息")
@Slf4j
public class VehiclesController {

    @Autowired
    private VehiclesService vehiclesService;

    /**
     * 根据车型id查询车辆信息列表
     * @param modelId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据车型id查询车辆信息")
    public Result list(Long modelId) {
        log.info("根据车型id查询车辆信息:{}",modelId);
        if (modelId == null){
            return Result.success(vehiclesService.list());
        }
        QueryWrapper<Vehicles> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("model_id",modelId);
        List<Vehicles> vehicles = vehiclesService.list(queryWrapper);
        return Result.success(vehicles);
    }

    /**
     * 分页查询车辆信息
     * @param vehiclesPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询车辆信息")
    public Result<PageResult> getPage(VehiclesPageQueryDTO vehiclesPageQueryDTO) {
        log.info("分页查询车辆信息:{}",vehiclesPageQueryDTO);
        PageResult pageResult = vehiclesService.pageQuery(vehiclesPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取车辆信息")
    public Result<VehiclesVO> getInfo(@PathVariable Long id) {
        log.info("获取车辆信息:{}",id);
        VehiclesVO vehiclesVO = vehiclesService.getCarInfo(id);
        return Result.success(vehiclesVO);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加车辆信息")
    public Result add(@RequestBody VehiclesDTO vehiclesDTO){
        log.info("添加车辆信息:{}",vehiclesDTO);
        vehiclesService.add(vehiclesDTO);
        return Result.success();
    }

    /**
     * 批量删除车辆信息
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "批量删除车辆信息")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除车辆信息:{}",ids);
        vehiclesService.delete(ids);
        return Result.success();
    }

    /**
     * 修改车辆信息
     * @param vehiclesVO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改车辆信息")
    public Result update(@RequestBody VehiclesVO vehiclesVO) {
        log.info("修改车辆信息:{}",vehiclesVO);
        vehiclesService.updateVehicles(vehiclesVO);
        return Result.success();
    }
}
